const { Random } = require('mockjs')
const { join } = require('path')
const fs = require('fs')

/**
 * @description:  随机生成图片
 * @param {*} width 图片宽度
 * @param {*} height 图片高度
 * @return {*}
 */
const handleRandomImage = (width = 50, height = 50) => `https://picsum.photos/${width}/${height}?random=${Random.guid()}`

/**
 * @description: 导出controller下所有mock的接口
 * @param {*}
 * @return {*}
 */
const handleMockArray = () => {
    const mockArray = []
    const getFiles = jsonPath => {
        const jsonFiles = []
        const findJsonFile = path => {
            const files = fs.readdirSync(path)
            files.forEach(item => {
                const fPath = join(path, item)
                const stat = fs.statSync(fPath)
                if (stat.isDirectory() === true) findJsonFile(item)
                if (stat.isFile() === true) jsonFiles.push(item)
            })
        }
        findJsonFile(jsonPath)
        jsonFiles.forEach(item => mockArray.push(`./controller/${item}`))
    }
    getFiles('mock/controller')
    return mockArray
}
const handleResponse = data => {
    return {
        code: 1,
        msg: '',
        successful: true,
        result: data
    }
}
module.exports = {
    handleRandomImage,
    handleMockArray,
    handleResponse
}
